package com.ptu.mq;


import com.ptu.config.RabbitMQConfig;
import com.ptu.entity.SeckillOrder;
import com.ptu.server.OrderServer;
import com.ptu.server.SeckillOrderServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SeckillOrderConsumer {

    @Autowired
    SeckillOrderServer seckillOrderServer;

    @RabbitListener(queues = RabbitMQConfig.SECKILL_ORDER_QUEUE)
    public void receiveSeckillOrder(SeckillOrder seckillOrder) {
        System.out.println("接收到订单，开始异步下单" + seckillOrder);
        try {
            seckillOrderServer.handleVoucherOrder(seckillOrder);
        } catch (Exception e) {
            System.out.println("处理秒杀订单异常" + e);
//            log.error("处理秒杀订单异常", e);
            // 这里可以添加重试逻辑或者将失败的消息存入数据库
        }
    }
}